package com.panshi.repast.ImageCache;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.panshi.repast.Cache.Cache;
import com.panshi.repast.Cache.DefaultCache;
import com.panshi.repast.Util.ThumbnailUtil;
import com.panshi.repast.Util.Utils;

/**
 * 异步缓存数据，内存
 * 
 * @author Administrator
 * 
 */
public class CacheMemUtil {

	// 缓存大小
	public static final long DEFAULT_CACHE_SIZE = Math.min(Runtime.getRuntime()
			.maxMemory() / 4, 30 * 1024 * 1024);

	private final static String imageCacheName = "iidear";
	/**
	 * 内存缓存
	 */
	private static Cache<String, Bitmap> cacheImage = null;
	private static String rootDir = null;

	static {
		cacheImage = new DefaultCache<String, Bitmap>(imageCacheName,
				DEFAULT_CACHE_SIZE, 0);
		rootDir = Utils.getCacheDir(imageCacheName);
		if (rootDir != null) {
			CacheFileUtil.maintainCacheFile(rootDir);
		}
	}

	/**
	 * 插入图片缓存
	 * 
	 * @param name
	 * @param img
	 */
	public static void putImage2Cache(String name, Bitmap img) {
		cacheImage.put(name, img);
	}

	/**
	 * 根据名称从cache中获取图片（第一步，主线程，直接加载�?
	 * 
	 * @param name
	 * @return
	 */
	public static Bitmap getImageFromCache(String name) {
		if(cacheImage.get(name) == null){
			return getImage(name);
		}
		return cacheImage.get(name);
	}

	/**
	 * 根据名称从cache中清理图�?
	 * 
	 * @param name
	 *            *
	 */
	public static void removeImageFromCache(String name) {
		cacheImage.remove(name);
	}

	/**
	 * 从本地获取图片（第二步，异步，避免滑动时发卡�?
	 * 
	 * @param name
	 * @return
	 */
	public static Bitmap getImage(String name) {
		Bitmap bm = CacheFileUtil.loadImageFromFile(rootDir, name); // 到本地找
		if (bm != null) {
			putImage2Cache(name, bm);
		}
		return bm;
	}

	public static String getImagePath(String name) {
		return CacheFileUtil.getImagePathForRoot(rootDir, name);
	}

	/**
	 * 把图片缓存到本地和内存
	 * 
	 * @param name
	 * @return
	 */
	public static void putImage(String name, byte[] data) {
		try {
			Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
			putImage2Cache(name, bm);
			CacheFileUtil
					.saveByte2SDCard(Utils.bitmap2Bytes(bm), rootDir, name);
		} catch (OutOfMemoryError oom) {
			clearCache();
			Bitmap bm = ThumbnailUtil.getImageThumbnail(data, 400, 400);
			CacheFileUtil
					.saveByte2SDCard(Utils.bitmap2Bytes(bm), rootDir, name);
			// clearCache();
			putImage2Cache(name, bm);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 把图片从本地和内存中清除
	 */
	public static void removeImage(String name) {
		removeImageFromCache(name);
		CacheFileUtil.removeFileByFileName(rootDir, name);
	}

	/**
	 * 清除cache
	 */
	public static void clearCache() {
		cacheImage.clear();
	}

	/**
	 * 清除内存图片缓存
	 */
	public static void removeImages() {
		CacheFileUtil.clearFiles(rootDir);
		clearCache();
	}

	/**
	 * 返回本地缓存图片容量
	 * 
	 * @return
	 */
	public static String getDirSize() {
		try {
			return CacheFileUtil.FormetFileSize(rootDir);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

}
